package com.ruoyi.doorcontrol.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.doorcontrol.service.HKEventService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.doorcontrol.domain.ThDoorControl;
import com.ruoyi.doorcontrol.service.IThDoorControlService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 门禁管理Controller
 * 
 * @author zhang
 * @date 2024-07-13
 */
@RestController
@RequestMapping("/doorcontrol/control")
public class ThDoorControlController extends BaseController
{
    @Autowired
    private IThDoorControlService thDoorControlService;
    @Autowired
    private HKEventService hkEventService;

    /**
     * 查询门禁管理列表
     */
    @PreAuthorize("@ss.hasPermi('doorcontrol:control:list')")
    @GetMapping("/list")
    public TableDataInfo list(ThDoorControl thDoorControl)
    {
        startPage();
        List<ThDoorControl> list = thDoorControlService.selectThDoorControlList(thDoorControl);
        return getDataTable(list);
    }

    /**
     * 导出门禁管理列表
     */
    @PreAuthorize("@ss.hasPermi('doorcontrol:control:export')")
    @Log(title = "门禁管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ThDoorControl thDoorControl)
    {
        List<ThDoorControl> list = thDoorControlService.selectThDoorControlList(thDoorControl);
        ExcelUtil<ThDoorControl> util = new ExcelUtil<ThDoorControl>(ThDoorControl.class);
        util.exportExcel(response, list, "门禁管理数据");
    }

    /**
     * 获取门禁管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('doorcontrol:control:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(thDoorControlService.selectThDoorControlById(id));
    }

    /**
     * 新增门禁管理
     */
    @PreAuthorize("@ss.hasPermi('doorcontrol:control:add')")
    @Log(title = "门禁管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ThDoorControl thDoorControl)
    {
        return toAjax(thDoorControlService.insertThDoorControl(thDoorControl));
    }

    /**
     * 修改门禁管理
     */
    @PreAuthorize("@ss.hasPermi('doorcontrol:control:edit')")
    @Log(title = "门禁管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ThDoorControl thDoorControl)
    {
        return toAjax(thDoorControlService.updateThDoorControl(thDoorControl));
    }

    /**
     * 删除门禁管理
     */
    @PreAuthorize("@ss.hasPermi('doorcontrol:control:remove')")
    @Log(title = "门禁管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(thDoorControlService.deleteThDoorControlByIds(ids));
    }


    /**
     * 门禁机登录
     * @return
     */
    @GetMapping("/login")
    public AjaxResult login(Long id){
        AjaxResult login = thDoorControlService.login(id);
        return login;
    }

    /**
     * 退出
     * @param id
     * @return
     */
    @GetMapping("/logout")
    public AjaxResult logout(Long id){
        AjaxResult ajaxResult = thDoorControlService.logout2(id);
        return ajaxResult;
    }

    /**
     * 布防
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("subscribe")
    public AjaxResult subscribe(Long id) throws Exception {
        return hkEventService.startSubscribe(id);
    }

    /**
     * 撤防
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("unsubscribe")
    public AjaxResult insertCard(Long id) throws Exception {
        return hkEventService.stopSubscribe(id);
    }
    /***
     * 根据门禁id远程控门
     * @param id
     * @return
     */
    @GetMapping("/remoteControl")
    public AjaxResult remoteControl(String id){
        return hkEventService.remoteControl(id);
    }

    /**
     * 远程开道闸
     * @return
     */
    @GetMapping("/remoteGate")
    public AjaxResult remoteGate(){
        return hkEventService.remoteGate();
    }






}
